import React from 'react';
import { observer, inject } from 'mobx-react';
import WfInput from '../../public/wf-form-input/index';
import WfSelect from '../../public/wf-form-select/index';
import * as Util from '../../util/formUtil';
import { getLabel } from '../../util/common';
import { _,FieldUtil } from '../../util/pcPublic';
import {toJS} from 'mobx';
@inject('wfGlobalStore','wfLayoutStore')
@observer
export default class SystemElement extends React.Component{
    constructor(props) {
        super(props);
        this.state = {
        };
    }
    componentWillMount() {
        const { wfLayoutStore} = this.props;
        const {mainData} = wfLayoutStore;
        this.setState({maindata: toJS(mainData)});
    }

    doChangeEvent(value){
        const { fieldMark,wfLayoutStore,wfGlobalStore } = this.props;
        wfLayoutStore.changeSingleFieldValue(fieldMark, {value: value});
        if(fieldMark == 'field-10') {//密级字段改变时，修改表单的当前密级，用于创建节点联动
            wfGlobalStore.setSecLevelInfo({secLevel : value});
        }
	}
    render() {
        const { fieldid,fieldMark,viewAttr,fieldAttr,symbol,fieldObj,wfGlobalStore,wfLayoutStore } = this.props;
        let value = this.props.fieldValue;
        const { commonParam,markInfo, secLevelInfo={} } = wfGlobalStore;
        const { containSignInput, propFileCfg={} } = wfLayoutStore;
        const { requestType } = commonParam;
        const fieldName  = FieldUtil.convertSystemFieldName(fieldMark);
        const { mobile_show_radio } = propFileCfg;
        const isview = viewAttr === 1;
        let options = [];
        if (fieldid == -1) {    //请求标题
            return (
                <WfInput
                    viewAttr={viewAttr}
                    fieldid="-1"
                    fieldMark={fieldName}
                    isDetail={false}
                    detailtype={1}
                    value={value}
                    onChange={this.doChangeEvent.bind(this)}
                />
            )
        } else if (fieldid == -2) {     //紧急程度
            //value = value == '' ? '0' : value;
            const {maindata} = this.state;
            let leveldata = [];
            if(maindata &&  maindata["field"+fieldid] &&  maindata["field"+fieldid].specialobj) {
                leveldata = maindata["field"+fieldid].specialobj;
            }
            if (isview) {
                let item = leveldata.filter(itm => itm.id == value);
                try{
                    options.push({value: item[0].id, label: item[0].customname});
                } catch (e) {
                }
            }else {
                leveldata && leveldata.map(item => {//循环获取数据
                    options.push({value: item.id, label: item.customname});
                })
            }
            /*options = [
                {value:'0', label:getLabel(225,'正常')},
                {value:'1', label:getLabel(25397,'重要')},
                {value:'2', label:getLabel(28090,'紧急')}
            ];*/
        } else if (fieldid == -3) {     //短信提醒
            value = value == '' ? '0' : value;
            options = [
                {value:'0', label:getLabel(17583,'不短信提醒')},
                {value:'1', label:getLabel(17584,'离线短信提醒')},
                {value:'2', label:getLabel(17585,'在线短信提醒')}
            ];

        } else if (fieldid == -4) {     //签字意见
            
        } else if (fieldid == -5) {     //微信提醒
            value = value == '' ? '0' : value;
            options = [
                {value:'0', label:getLabel(19782,'不提醒')},
                {value:'1', label:getLabel(15148,'提醒')},
            ];
        } else if (fieldid == -9) {     //打印次数
            return <span>{_.trim(value).length === 0 ? "0" : value}</span>
        } else if(fieldid == -10) {  //密级
            if(isview) {
                return <span>{secLevelInfo.secLevelName}</span>
            } else {
                secLevelInfo.secLevelOption && secLevelInfo.secLevelOption.map((item,index) =>{
                    item.value = item.key;
                    item.label = item.showname;
                });
                return (
                    <WfSelect
                        domKey={fieldMark}
                        name={fieldid}
                        viewAttr={secLevelInfo.canSecChange ? 3 : 1}
                        value={secLevelInfo.secLevel}
                        options={secLevelInfo.secLevelOption}
                        onChange={this.doChangeEvent.bind(this)}
                        mobile_show_radio={mobile_show_radio}
                        fieldAttr={fieldAttr}
                    />
                )
            }
        }
        if(fieldid == -2 || fieldid == -3 || fieldid == -5)
            return (
                <WfSelect
                    domKey={fieldMark}
                    name={fieldid}
                    viewAttr={viewAttr}
                    value={value}
                    options={options}
                    onChange={this.doChangeEvent.bind(this)}
                    mobile_show_radio={mobile_show_radio}
                    fieldAttr={fieldAttr}
                />
            )
        else
            return <div></div>
    }
}